/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.mvp.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import tpdisenio2009.Tpdisenio2009App;
import tpdisenio2009.model.beans.FantasyTeamPlayer;
import tpdisenio2009.model.beans.FantasyTeamScoreByTournamentDate;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.beans.User;
import tpdisenio2009.mvp.display.WinnersPrinterPreviewView;
import tpdisenio2009.services.FantasyTeamPlayerService;
import tpdisenio2009.services.FantasyTeamScoreByTournamentDateService;

/**
 *
 * @author Pablito
 */
public class WinnersPrinterPreviewPresenter extends DefaultPresenter<WinnersPrinterPreviewView> {
        
    private final FantasyTeamScoreByTournamentDateService fantasyTeamScoreService;
    private final FantasyTeamPlayerService fantasyTeamPlayerService;

    private ActionListener printerListener;
    private ActionListener exitListener;

    public WinnersPrinterPreviewPresenter( WinnersPrinterPreviewView view ) {
        super( view );
        
        fantasyTeamScoreService = new FantasyTeamScoreByTournamentDateService();
        fantasyTeamPlayerService = new FantasyTeamPlayerService();
        
        if ( Tpdisenio2009App.loggedUser instanceof User) {
            User user = (User) Tpdisenio2009App.loggedUser;
            getView().setDate( getView().getDate() );
            getView().setFantasyTeamName( user.getFantasyTeam().getName() );
            getView().setOwnPoints( fantasyTeamScoreService.getFantasyTeamPointsByDate( user.getFantasyTeam() , getView().getDate() ) );
            getView().setOwnPosition( fantasyTeamScoreService.getFantasyTeamPositionByDate( user.getFantasyTeam() , getView().getDate() ) );
            loadFantasyTeamsTable( );            

            bind();
        }else {
            getView().showNotification( "<html> Su usuario es incorrecto. <br>Compruebe que no sea un Administrador o un Periodista.</html>", "Usuario Incorrecto" );
        }
    }

    public void bind() {

        printerListener = new ActionListener() {
            public void actionPerformed( ActionEvent e ) {                
                getView().showNotification( "<html>A continuación se imprime la<br>lista que se visualiza aquí.</html>", "Imprimiendo" );
            }
        };
        getView().addPrinterListListener( printerListener );
        
        exitListener = new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                getView().close();
            }
        };
        getView().addExitListener( exitListener );
       
    }

    public void unbind() {
        if (printerListener != null)
            getView().removePrinterListListener( printerListener );
        if (exitListener != null)
            getView().removeExitListener( exitListener );
    }

    private void loadFantasyTeamsTable( ) {
        TournamentDate date = getView().getDate();
        List<FantasyTeamScoreByTournamentDate> fantasyTeamScores = fantasyTeamScoreService.getFantasyTeamScoresByTournamentDate( date );
        List<FantasyTeamPlayer> bestPlayers = fantasyTeamPlayerService.getBestPlayers(fantasyTeamScores, date);
        getView().loadFantasyTeams( fantasyTeamScores, bestPlayers );
    }

}
